import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用黑体显示中文
plt.rcParams['axes.unicode_minus'] = False    # 正常显示负号

# 简化的圆周反射类
class CircleReflection:
    def __init__(self, center=0, radius=1):
        self.center, self.radius = complex(center), float(radius)
    
    def reflect(self, z):
        if abs(z - self.center) < 1e-12:
            return np.inf
        return self.center + (self.radius**2) / np.conj(z - self.center)
    
    def reflect_point(self, z):
        return self.center if np.isinf(z) else self.reflect(z)

# 根据习题4，求虚轴、直线x=y、单位圆周关于圆周|z-2|=1的反射
print("实验二：圆周反射变换")
reflection_circle = CircleReflection(center=2, radius=1)

# 1. 虚轴的反射（x=0）
y_vals = np.linspace(-3, 3, 50)
points_on_imag_axis = 1j * y_vals
reflected_imag_axis = [reflection_circle.reflect_point(z) for z in points_on_imag_axis]
reflected_imag_axis_finite = [w for w in reflected_imag_axis if not np.isinf(w)]
reflected_real_parts = [np.real(w) for w in reflected_imag_axis_finite]
reflected_imag_parts = [np.imag(w) for w in reflected_imag_axis_finite]

# 2. 直线x=y的反射
t_vals = np.linspace(-3, 3, 50)
points_on_line = t_vals + 1j * t_vals
reflected_line = [reflection_circle.reflect_point(z) for z in points_on_line]
reflected_line_finite = [w for w in reflected_line if not np.isinf(w)]
reflected_line_real = [np.real(w) for w in reflected_line_finite]
reflected_line_imag = [np.imag(w) for w in reflected_line_finite]

# 3. 单位圆周的反射
theta_vals = np.linspace(0, 2*np.pi, 50)
points_on_unit_circle = np.exp(1j * theta_vals)
reflected_unit_circle = [reflection_circle.reflect_point(z) for z in points_on_unit_circle]
reflected_unit_circle_finite = [w for w in reflected_unit_circle if not np.isinf(w)]
reflected_unit_circle_real = [np.real(w) for w in reflected_unit_circle_finite]
reflected_unit_circle_imag = [np.imag(w) for w in reflected_unit_circle_finite]

# 可视化
fig, ax = plt.subplots(1, 1, figsize=(10, 10))
circle_theta = np.linspace(0, 2*np.pi, 100)
reflection_circle_points = 2 + np.exp(1j * circle_theta)
ax.plot(np.real(reflection_circle_points), np.imag(reflection_circle_points), 'k-', linewidth=2, label='反射圆周 |z-2|=1')
ax.axvline(0, color='blue', linestyle='--', linewidth=1, label='虚轴 x=0')
line_x = np.linspace(-3, 3, 100)
ax.plot(line_x, line_x, color='green', linestyle='--', linewidth=1, label='直线 x=y')
unit_circle_points = np.exp(1j * circle_theta)
ax.plot(np.real(unit_circle_points), np.imag(unit_circle_points), color='orange', linestyle='--', linewidth=1, label='单位圆周 |z|=1')
ax.plot(reflected_real_parts, reflected_imag_parts, color='blue', linewidth=1.5, label='虚轴的反射')
ax.plot(reflected_line_real, reflected_line_imag, color='green', linewidth=1.5, label='直线x=y的反射')
ax.plot(reflected_unit_circle_real, reflected_unit_circle_imag, color='orange', linewidth=1.5, label='单位圆周的反射')
ax.scatter([2], [0], color='black', s=50, label='反射圆心 (2,0)')
ax.set_xlim(-4, 6); ax.set_ylim(-4, 4); ax.set_title('圆周反射变换'); ax.grid(True); ax.legend()
plt.axis('equal'); plt.show()

# 验证反射将圆周变为圆周（习题3）
print("\n验证反射将圆周变为圆周:")
reflection = CircleReflection(center=0, radius=1)
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
circles = [{"center": 0, "radius": 1, "name": "单位圆周"}, {"center": 1+1j, "radius": 0.5, "name": "圆周1"}]

for circle in circles:
    center, radius, name = circle["center"], circle["radius"], circle["name"]
    theta = np.linspace(0, 2*np.pi, 50)
    circle_points = center + radius * np.exp(1j * theta)
    reflected_points = [reflection.reflect_point(z) for z in circle_points]
    reflected_points_finite = [w for w in reflected_points if not np.isinf(w)]
    ax.plot(np.real(circle_points), np.imag(circle_points), linestyle='--', linewidth=1, alpha=0.7)
    if len(reflected_points_finite) > 0:
        ax.plot(np.real(reflected_points_finite), np.imag(reflected_points_finite), linewidth=2, label=f'{name}的反射')
    print(f"{name}经过反射后仍为圆周")

ax.set_xlim(-3, 3); ax.set_ylim(-3, 3); ax.set_title('反射将圆周变为圆周'); ax.grid(True); ax.legend()
plt.axis('equal'); plt.show()